
class_name CPlayerSkillArray
extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal MaskID_changed(old_value, new_value) 
signal ClassSkillID_changed(old_value, new_value) 
signal SpecialID_changed(old_value, new_value) 
signal Level_changed(old_value, new_value) 
signal UnlockSPID_changed(old_value, new_value) 
signal ActivePos_changed(old_value, new_value) 



class SPlayerSkill:
    var nIndex
    var tblOwner
    var m_tblAlias
    var bInited = false
    func _init(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.ClassSkillID = 0	# 职业技能ID
        self.SpecialID = 0	# 特化技能ID
        self.Level = 0	# 技能等级

        self.UnlockSPID = []
        self.UnlockSPID.resize(10)
        for i in range(10):
            self.UnlockSPID[i] = 0
        self.ActivePos = 0	# 上阵位置

        self.nIndex = -1
        self.tblOwner = owner
        self.m_tblAlias = {
 
        }

    func GetIndex():
        return self.nIndex

    func SetZeroValue(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.ClassSkillID = 0	# 职业技能ID
        self.SpecialID = 0	# 特化技能ID
        self.Level = 0	# 技能等级

        self.UnlockSPID = []
        self.UnlockSPID.resize(10)
        for i in range(10):
            self.UnlockSPID[i] = 0
        self.ActivePos = 0	# 上阵位置


    func UnPack(_bufferStream, _bOffset):
        if _bOffset :
            _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)

        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.ClassSkillID = _bufferStream.ReadUint32()
        self.SpecialID = _bufferStream.ReadUint32()
        self.Level = _bufferStream.ReadUint8()
        for i in range(TableCommonDefine.ECharLen.eCL_UnlockSPID):
            self.UnlockSPID[i-1] = _bufferStream.ReadUint32()
        self.ActivePos = _bufferStream.ReadUint8()

        self.bInited = true
        if self.tblOwner :
            self.tblOwner.DispatchEvent('uuid', [self, 0, self.nIndex])

    func CopyFrom(other):
        self.nIndex = other.nIndex
        self.ClassSkillID = other.ClassSkillID
        self.SpecialID = other.SpecialID
        self.Level = other.Level
        for i in range(10):
            self.UnlockSPID[i] = other.UnlockSPID[i]
        self.ActivePos = other.ActivePos


    func Update(_bufferStream, _nIndex ):
        _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)
        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.ClassSkillID = _bufferStream.ReadUint32()
        self.SpecialID = _bufferStream.ReadUint32()
        self.Level = _bufferStream.ReadUint8()
        var bChanged = false
        var newUnlockSPID = {}
        for i in range(TableCommonDefine.ECharLen.eCL_UnlockSPID):
            newUnlockSPID[i-1] = _bufferStream.ReadUint32()        
            if not bChanged and self.m_UnlockSPID[i] != newUnlockSPID[i]:
                bChanged = true
        var oldUnlockSPID = self.m_UnlockSPID
        self.m_UnlockSPID = newUnlockSPID
        if bChanged and self.tblOwner:
            self.tblOwner.DispatchEvent('UnlockSPID', [self, oldUnlockSPID, newUnlockSPID])
        self.ActivePos = _bufferStream.ReadUint8()


    func OnDataChanged( fieldName, oldValue, newValue ):
        if oldValue == newValue or not self.tblOwner:
            return
        self.tblOwner.emit_signal(fieldName, oldValue, newValue)


    #  SPlayerSkill Geter Setter method # 
    var UUID:
        set(_value):
            var old = self.m_UUID
            self.UUID = _value
            if self.tblOwner:
                self.tblOwner.UUID_changed(old, _value)
        get:
            return UUID

    var MaskID:
        set(_value):
            var old = self.m_MaskID
            self.MaskID = _value
            if self.tblOwner:
                self.tblOwner.MaskID_changed(old, _value)
        get:
            return MaskID

    var ClassSkillID:
        set(_value):
            var old = self.m_ClassSkillID
            self.ClassSkillID = _value
            if self.tblOwner:
                self.tblOwner.ClassSkillID_changed(old, _value)
        get:
            return ClassSkillID

    var SpecialID:
        set(_value):
            var old = self.m_SpecialID
            self.SpecialID = _value
            if self.tblOwner:
                self.tblOwner.SpecialID_changed(old, _value)
        get:
            return SpecialID

    var Level:
        set(_value):
            var old = self.m_Level
            self.Level = _value
            if self.tblOwner:
                self.tblOwner.Level_changed(old, _value)
        get:
            return Level

    var UnlockSPID:
        set(_value):
            var old = self.m_UnlockSPID
            self.UnlockSPID = _value
            if self.tblOwner:
                self.tblOwner.UnlockSPID_changed(old, _value)
        get:
            return UnlockSPID

    func GetUnlockSPIDByIndex(_nIndex):
        return self.m_UnlockSPID[_nIndex]
    
    func SetUnlockSPIDByIndex(_value,_nIndex):
        var old = self.m_UnlockSPID[_nIndex]    
        self.m_UnlockSPID[_nIndex] = _value
        # self.OnDataChanged( "UnlockSPID", _nIndex, [old, _value])
        # self.OnDataChanged( "UnlockSPID", old, _value, _nIndex )    
        return 5

    var ActivePos:
        set(_value):
            var old = self.m_ActivePos
            self.ActivePos = _value
            if self.tblOwner:
                self.tblOwner.ActivePos_changed(old, _value)
        get:
            return ActivePos



# ================================= array ==========================
var m_UUID
var m_nPlayerSkillCount
var m_PlayerSkill

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nPlayerSkillCount = 0
    self.m_PlayerSkill = []
    var nMaxIndex = self.GetPlayerSkillMaxCount()-1
    self.m_PlayerSkill.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_PlayerSkill[i] = SPlayerSkill.new(self) 
        self.m_PlayerSkill[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nPlayerSkillCount = _bufferStream.ReadUint32()
    for i in range(self.m_nPlayerSkillCount):
        var elem = SPlayerSkill.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_PlayerSkill[i] = elem

func GetTableName():
    return 'PlayerSkill'

func GetUUID():
    return self.m_UUID

func GetPlayerSkillCurNum():
    return self.m_nPlayerSkillCount

func SetPlayerSkillCurNum( nValue ):
    self.m_nPlayerSkillCount = nValue

func SetCurNum(nValue):
    self.m_nPlayerSkillCount = nValue

static func GetPlayerSkillMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL

func GetPlayerSkillMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL

func GetPlayerSkill(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL:
        print("GetPlayerSkillEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL")
        return null

    return self.m_PlayerSkill[_nIndex];

func SetPlayerSkill(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL:
        print("SetPlayerSkillEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL")
        return null
    
    var old = self.m_PlayerSkill[_nIndex]
    var elem = SPlayerSkill.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_PlayerSkill[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddPlayerSkill(_Prop, _nIndex):
    var nCurNum = self.m_nPlayerSkillCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL:
        print("AddPlayerSkillEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL");
        return null;

    var elem = SPlayerSkill.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_PlayerSkill[i] = self.m_PlayerSkill[i - 1]
            self.m_PlayerSkill[i].nIndex = i
    self.m_PlayerSkill[_nIndex] = elem
    self.m_nPlayerSkillCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelPlayerSkill(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nPlayerSkillCount:
        return
    
    self.m_nPlayerSkillCount = self.m_nPlayerSkillCount - 1
    if _nIndex != self.m_nPlayerSkillCount:
        self.m_PlayerSkill[_nIndex] = self.m_PlayerSkill[self.m_nPlayerSkillCount]

    # var nLastDataID = self.m_PlayerSkill[self.m_nPlayerSkillCount]
    var elem = self.m_PlayerSkill[_nIndex]
    for i in range(_nIndex + 1, self.m_nPlayerSkillCount - 1):
        self.m_PlayerSkill[i - 1] = self.m_PlayerSkill[i]
        self.m_PlayerSkill[i - 1].nIndex = i - 1
    self.m_PlayerSkill[self.m_nPlayerSkillCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nPlayerSkillCount = _pOther.m_nPlayerSkillCount
    for i in range(_pOther.m_nPlayerSkillCount):
        self.m_PlayerSkill[i] = SPlayerSkill.new(self)
        self.m_PlayerSkill[i].CopyFrom(_pOther.m_PlayerSkill[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nPlayerSkillCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL:
        # var szLog = string.format("CPlayerSkillArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxPlayerSkillEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSKILL)
        return false
    var elem = null
    var old = null
    if self.m_PlayerSkill[_fieldId] != null:
        elem = self.m_PlayerSkill[_fieldId]
        old = SPlayerSkill.new( self )
        old.CopyFrom( elem )
    else:
        elem = SPlayerSkill.new(self)
        self.m_PlayerSkill[_fieldId] = elem
        self.m_PlayerSkill[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nPlayerSkillCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = SPlayerSkill.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_PlayerSkill[_fieldId]:
        self.m_PlayerSkill[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nPlayerSkillCount, _fieldId+1, -1):
            self.m_PlayerSkill[index] = self.m_PlayerSkill[index- 1]
            self.m_PlayerSkill[index].m_UUID = uuid + index - _fieldId
            self.m_PlayerSkill[index].nIndex = index
        self.m_PlayerSkill[_fieldId] = elem;
        self.m_PlayerSkill[_fieldId].nIndex = _fieldId

    self.m_nPlayerSkillCount = self.m_nPlayerSkillCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_PlayerSkill[_fieldId] == null:
        return 
    
    self.m_nPlayerSkillCount = self.m_nPlayerSkillCount - 1
    var elem = self.m_PlayerSkill[_fieldId]
    if _fieldId != self.m_nPlayerSkillCount:
        var uuid = self.m_PlayerSkill[_fieldId].m_UUID;
        self.m_PlayerSkill[_fieldId] = self.m_PlayerSkill[self.m_nPlayerSkillCount]
        self.m_PlayerSkill[_fieldId].m_UUID = uuid
        self.m_PlayerSkill[_fieldId].nIndex = _fieldId

    self.m_PlayerSkill[self.m_nPlayerSkillCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nPlayerSkillCount-1):
        if funFilter.call(self.m_PlayerSkill[i], i):
            tblItems.append(self.m_PlayerSkill[i])
    return tblItems

func QueryByClassSkillID(ClassSkillID):
    var nCnt = self.m_nPlayerSkillCount
    for i in range(nCnt-1):
        var curElem = self.m_PlayerSkill[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_ClassSkillID == ClassSkillID :
                return curElem
    return null


